package cli;

import database.DatabaseHelper;
import entity.Course;
import entity.Section;
import statistic.StudentScoreList;
import sun.plugin.dom.core.CoreConstants;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Pattern;

public class CourseScore implements CommandLineInterface.Handler {

    private DatabaseHelper db;

    private Course inputCourse(Scanner in, PrintWriter out) throws SQLException {
        out.println("请输入课程号或课程名：");
        String str = in.next();
        Course cou;
        if (str.matches("[a-zA-Z0-9]*[0-9][a-zA-Z0-9]*")) {
            cou = Course.find(db, str);
        } else {
            ArrayList<Course> courses = Course.findByName(db, str);
            if (courses.isEmpty()) {
                cou = null;
            } else if (courses.size() == 1) {
                cou = courses.get(0);
            } else {
                out.println("有多个课程重名：");
                String[] header = {"课程号", "课程名"};
                String[][] table = new String[courses.size()][header.length];
                for (int i = 0; i < courses.size(); ++i) {
                    table[i][0] = courses.get(i).getId();
                    table[i][1] = courses.get(i).getName();
                }
                out.print(new TableLayout(header, table));
                out.println("请输入课程号：");
                cou = Course.find(db, in.next());
            }
        }
        return cou;
    }

    private StudentScoreList.Order inputOrder(Scanner in, PrintWriter out) {
        out.println("学生按学号排序，还是按成绩排序？");
        String order = in.next();
        return order.contains("学号") ? StudentScoreList.Order.ID : StudentScoreList.Order.SCORE;
    }

    private void output(PrintWriter out, StudentScoreList list) {
        if (list == null)
            return;
        String[] header = {"学号", "姓名", "平时成绩", "期中成绩", "实验成绩", "期末成绩", "总成绩", "取得时间"};
        String[][] table = new String[list.students.size()][header.length];
        for (int i = 0; i < list.students.size(); ++i) {
            table[i][0] = list.students.get(i).getId();
            table[i][1] = list.students.get(i).getName();
            table[i][2] = String.format("%.0f", list.scores.get(i).getUsual());
            table[i][3] = String.format("%.0f", list.scores.get(i).getMidterm());
            table[i][4] = String.format("%.0f", list.scores.get(i).getExperiment());
            table[i][5] = String.format("%.0f", list.scores.get(i).getTerminal());
            table[i][6] = String.format("%.0f", list.scores.get(i).getTotal());
            table[i][7] = list.scores.get(i).getTimeStamp().format(DateTimeFormatter.ofPattern("yyyy年MM月dd日"));
        }
        out.println(new TableLayout(header, table));
    }

    @Override
    public void handle(Scanner in, PrintWriter out) {
        try {
            db = new DatabaseHelper();
            Course cou = inputCourse(in, out);
            if (cou == null) {
                out.println("课程不存在！");
                return;
            }
            StudentScoreList.Order ord = inputOrder(in, out);
            StudentScoreList list = new StudentScoreList(cou, ord);
            output(out, list);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            db.close();
        }
    }
}
